package com.houtai.controller;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.web.bind.annotation.*;


import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 电子轨迹
 */
@RestController
@RequestMapping("sys/lineboxs")
public class LineBoxs {

    @Autowired
    private JdbcTemplate jdbc;

    @RequestMapping(value = "/getall")
    public Object getall(){
        String  sql = "select * from mylines";
        return jdbc.queryForList(sql);
    }

    @RequestMapping(value = "/query",method = RequestMethod.POST)
    public List<Double> query(@RequestBody Object obj){
        if(obj==null){
            System.out.println("obj为空");
            return null;
        }
        List olist = (ArrayList)obj;

        Long timeLong = Long.parseLong(String.valueOf(olist.get(0)));
        Long timeLong1 = Long.parseLong(String.valueOf(olist.get(1)));
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = null;
        Date date1 = null;
        try {
            date = sdf.parse(sdf.format(timeLong));
            date1 = sdf.parse(sdf.format(timeLong1));
        } catch (ParseException e) {
            e.printStackTrace();
        }

        String  sql = "select * from mylines where time between ? and ? order by id asc";
        List<Map<String, Object>> lists = jdbc.queryForList(sql, date, date1);
        List<Double> resdata = new ArrayList<>();

        for(int i=0;i<lists.size();i++) {
            resdata.add(Double.parseDouble(lists.get(i).get("jposition").toString()));
            resdata.add(Double.parseDouble(lists.get(i).get("wposition").toString()));
        }
        return  resdata;
    }
}
